OUTPUT_DIR := target/
CEPH_PROXY_DIR := cmd/cephproxy
CEPH_PROXY_OUT_NAME := ceph_proxy
CEPH_RPXOY_ADMIN_DIR := cmd/admin
CEPH_PROXY_ADMIN_NAME := ceph-proxy-admin

VERSION_PACKAGE := github.com/mars/pkg/app
GIT_BRANCH_NAME := $(shell git symbolic-ref --short -q HEAD)
GIT_COMMIT := $(shell git rev-parse HEAD)
BUILD_DATE := $(shell date +"%Y-%-m-%d %H:%M:%S")


GO_LDFLAGS += -X '$(VERSION_PACKAGE).GitVersion=$(GIT_BRANCH_NAME)' \
	-X '$(VERSION_PACKAGE).GitCommit=$(GIT_COMMIT)' \
	-X '$(VERSION_PACKAGE).BuildDate=$(BUILD_DATE)'

run-admin:
	cd cmd/admin && go run main.go

run-cephproxy:
	cd cmd/cephproxy && go run main.go

run-frontend:
	cd frontend && npm run start	

tidy:
	go mod tidy

test:
	go test ./...

build:
	@mkdir -p $(OUTPUT_DIR)
	@go build -ldflags "$(GO_LDFLAGS)" -o $(OUTPUT_DIR)$(CEPH_PROXY_OUT_NAME) $(CEPH_PROXY_DIR)/main.go
	@go build -ldflags "$(GO_LDFLAGS)" -o $(OUTPUT_DIR)$(CEPH_PROXY_ADMIN_NAME) $(CEPH_RPXOY_ADMIN_DIR)/main.go

.PHONY: frontend
frontend:
	@pushd frontend && npm run build && popd
	@pushd frontend && tar -cvf build.tar build && popd

.PHONY: clean
clean:
	@rm -rf $(OUTPUT_DIR)
	@rm -rf frontend/build && rm -f frontend/build.tar
